<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit ? '修改分类' : '新增分类'"
    width="800px"
    :visible.sync="visible"
  >
    <el-form label-width="80px" :model="form" ref="form" :rules="rules">
      <el-form-item label="父级分类">
        <classify-select
          :disabled="parentCheck"
          :style="{width: '435px',float: 'left'}"
          ref="classifySelect"
          :selectValue="form.parentId"
          @select="handleClassifySelect"
        />
        <el-checkbox class="fr" @change="handleCheckChange" v-model="parentCheck">顶级分类</el-checkbox>
      </el-form-item>
      <el-divider />
      <el-form-item label="分类名称" prop="typeName">
        <el-input style="width: 100%" v-model="form.typeName" placeholder="请输入分类名称" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleOk">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { openNotification } from '@/utils/util'
import { typeCreate } from '@/api/prewarning'
import ClassifySelect from './ClassifySelect'
export default {
  components: {
    ClassifySelect
  },
  data() {
    return {
      visible: false,
      form: {
        typeName: null
      },
      rules: {
        typeName: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      isEdit: false,
      parentCheck: false
    }
  },
  methods: {
    edit(bool, row) {
      this.visible = true
      this.isEdit = bool
    },
    handleOk() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          const res = await typeCreate(this.form)
          if (res.state === '000000') {
            openNotification('success', '新增操作成功')
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    },
    handleCheckChange(e) {
      console.log('e', e)
      if (e) {
        this.$set(this.form, 'parentId', null)
      }
    },
    handleClassifySelect(value) {
      this.$set(this.form, 'parentId', value.id)
    }
  }
}
</script>
